package com.itheima.sc.service.pms;

import cn.hutool.core.convert.Convert;
import com.itheima.sc.api.pms.CateAttri_RelaApi;
import com.itheima.sc.api.pms.ProductAttributeApi;
import com.itheima.sc.pojo.pms.ProductAttribute;
import com.itheima.sc.pojo.pms.ProductCategoryAttributeRelation;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductAttributeService {
    @DubboReference
    private ProductAttributeApi attributeApi;
    @DubboReference
    private CateAttri_RelaApi cateAttri_relaApi;


    //attributeCatory中的删除,相对应的attribute里根据catoryId删除
    public void deleteByCategoryId(Integer cid) {
        attributeApi.deleteByCategoryId(cid);
    }

    //获取cid对应的attr集合, 获取所有category时设置其附加属性
    public List<ProductAttribute> listByCategoryId(Long id) {
        List<ProductAttribute> attributeList = attributeApi.listByCategoryId(id);
        return attributeList;
    }

    //根据商品分类的id获取商品属性及属性分类
    public  List<Map<String,Integer>> attrInfo(Integer productCategoryId){
        //在Cate_Attri_Relation关系表中查询pcid对应的attriId
        List<ProductCategoryAttributeRelation> relationList = cateAttri_relaApi.genByCategoryId(productCategoryId);

        ArrayList<Map<String, Integer>> list = new ArrayList<>();
        for (ProductCategoryAttributeRelation relation : relationList) {
            //遍历关系对象,提取每个attriId
            Long attributeId = relation.getProductAttributeId();
            //调用方法在attri表中查询对应的唯一acid
            ProductAttribute attribute = attributeApi.getById(attributeId);
            Long attributeCategoryId = attribute.getProductAttributeCategoryId();
            //封装map
            Map<String, Integer> map = new HashMap<>();
            map.put("attributeCategoryId",Convert.toInt(attributeCategoryId));
            map.put("attributeId",Convert.toInt(attributeId));
            //装进list
            list.add(map);
        }
        return list;
    }

    //添加商品属性信息
    public void create(ProductAttribute attribute) {
        attributeApi.save(attribute);
    }

    //批量删除商品属性
    public void deleteByIds(List<Integer> ids) {
        attributeApi.removeByIds(ids);
    }

    //分页根据分类查询属性列表或参数列表
    public PageResult listByType(Integer cid, Integer type, Integer pageNum, Integer pageSize) {
        List<ProductAttribute> list = attributeApi.listByType(cid,type,pageNum,pageSize);

        //查询totalCount
        Integer total = attributeApi.getCount(cid,type,pageNum,pageSize);
        Integer totalPage = total/pageNum+1;
        //封装pageResult
        PageResult<ProductAttribute> pageResult = new PageResult<>(pageNum, pageSize, total, totalPage, list);
        return pageResult;
    }

    //修改商品属性信息
    public void updateById(ProductAttribute attribute) {
        attributeApi.updateById(attribute);
    }

    //查询单个商品属性
    public ProductAttribute getById(Integer id) {
        ProductAttribute attribute = attributeApi.getById(id);
        return attribute;
    }














}
